package com.jztchat.asyncTask.chatRecordTask;

import com.google.gson.Gson;
import com.jztchat.entity.ChatRecord;
import com.jztchat.redis.RedisKeyConstent;
import com.jztchat.repository.ChatRecordRepository;
import com.jztchat.util.ApplicationContextUtil;
import com.jztchat.webSocket.service.SocketConnectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * Created by zzy on 2018/1/30.
 */
@Slf4j
public class RecordChatTask implements Runnable{

        @Override
        public void run() {
            log.info("循环取值----");
            while (true) {
                if (null != RecordChatHandler.queue && RecordChatHandler.queue.size() > 0) {
                    ChatRecord chatRecord = RecordChatHandler.queue.poll();
                    ChatRecordRepository chatRecordRepository = ApplicationContextUtil.getBean(ChatRecordRepository.class);
                    if (chatRecordRepository != null && chatRecord != null) {
                        chatRecordRepository.save(chatRecord);
                        //同时将信息存入redis，当客户上线从redis取其未读消息，取不到再从数据库取（无太大意义，只是为了了解redis定于发布）
                        if(1!=chatRecord.getIsSend()) {//没有接收到的才存入redis
                            SocketConnectService socketConnectService = ApplicationContextUtil.getBean(SocketConnectService.class);
                            socketConnectService.setChatRecordToRids(chatRecord);
                        }
                    }
                }
            }
        }
}
